package com.dq.controller.op.opmerchant.orders;

import com.dq.annotation.aop.SysLog;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.orders.ShortageOrderQuery;
import com.dq.controller.in.orders.ShortageOrderSave;
import com.dq.vo.ListDataVo;
import com.dq.vo.Vo;

import com.dq.domain.orders.ShortageOrder;
import com.dq.domain.orders.info.OrderInfo;
import com.dq.domain.org.Org;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.service.AllService;
import com.dq.utils.BeanNullUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cf
 */
@Api(description = "缺货单管理")
@RestController
@RequestMapping("sys/shortageOrder/")
public class OrgShortageOrderController extends AllService {

    @ApiOperation("缺货单列表")
    @PostMapping("/list")
    @RequiresPermissions("sys:shortageOrder:list")
    public Vo shortageOrderList(@RequestBody ShortageOrderQuery shortageOrderQuery)throws AdminException{
        shortageOrderQuery.format();
        if(StringUtils.isBlank(shortageOrderQuery.getType())) {
            throw new AdminException("type 不能为空");
        }
        if(shortageOrderQuery.getShortageType() == null) {
            throw new AdminException("shortageType 不能为空");
        }

        Org org = null;
        if("org".equals(shortageOrderQuery.getType())) {
            SysUser user = sysUserService.getUser();
            if(user.getSection() == null || user.getSection().getId() == null) {
                throw new AdminException("登录用户 门店信息异常");
            }
            org = (Org) user.getSection();
        }else {
            if(shortageOrderQuery.getOrgId() != null) {
                org = dao.get(Org.class, shortageOrderQuery.getOrgId());
                if(org == null) {
                    throw new AdminException("orgId无对应门店信息");
                }
            }
        }

        List<ShortageOrder> shortageOrders = shortageOrderService.findByWhere(shortageOrderQuery, org);
        Long totalCount = shortageOrderService.countByWhere(shortageOrderQuery,org);
        List<ShortageOrder.ShortageOrderListVo> shortageOrderListVos = new ArrayList<>();
        shortageOrders.forEach(shortageOrder -> {shortageOrderListVos.add(shortageOrder.toShortageOrderListVo());});

        return new ListDataVo(shortageOrderListVos, totalCount);
    }

    @ApiOperation("缺货单详情商品列表")
    @GetMapping("/detail/{id}")
    @RequiresPermissions("sys:shortageOrder:list")
    public Vo shortageOrderDetail(@PathVariable @ApiParam("订单id") Long id)throws AdminException{
        ShortageOrder shortageOrder = dao.get(ShortageOrder.class,id);
        if(null==shortageOrder){throw new AdminException("该订单不存在");}

        List<OrderInfo> orderInfoList = orderInfoRepository.findByOrders(shortageOrder);
        List<OrderInfo.OrgOrderInfoVo> orgOrderInfoVos = new ArrayList<>();
        orderInfoList.forEach(orderInfo -> {orgOrderInfoVos.add(orderInfo.toOrgOrderInfoVo());});

        ShortageOrder.ShortageOrderListVo basicVo = shortageOrder.toShortageOrderListVo();

        basicVo.setOrgOrderInfoVos(orgOrderInfoVos);
        return new Vo(basicVo);
    }

    @ApiOperation("手动录入销售/采购缺货单")
    @PostMapping("/save")
    @RequiresPermissions("sys:shortageOrder:save")
    public Vo save(@ApiParam("") @RequestBody ShortageOrderSave shortageOrderSave) throws AdminException {

        shortageOrderService.manualSave(shortageOrderSave);

        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("缺货单确认补货")
    @PostMapping("/affirm/{id}")
    @RequiresPermissions("sys:shortageOrder:update")
    public Vo affirm(@PathVariable @ApiParam(value = "id") Long id) throws AdminException {
        ShortageOrder orgPositionQuery = dao.get(ShortageOrder.class, id);
        if (orgPositionQuery == null) {
            throw new AdminException("无对应id对象");
        }

        orgPositionQuery.setShortageStatus(ShortageOrder.ShortageStatus.alreadyReplenish);
        shortageOrderService.update(orgPositionQuery);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    @RequiresPermissions("sys:shortageOrder:update")
    public Vo update(@RequestBody @ApiParam(value = "") ShortageOrder shortageOrder) throws AdminException {
        if(shortageOrder.getId() == null) {
            throw new AdminException("缺参id");
        }

        ShortageOrder orgPositionQuery = dao.get(ShortageOrder.class, shortageOrder.getId());
        if (orgPositionQuery == null) {
            throw new AdminException("无对应id对象");
        }
        BeanUtils.copyProperties(shortageOrder, orgPositionQuery, BeanNullUtil.getNullPropertyNames(shortageOrder));
        shortageOrderService.update(orgPositionQuery);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("删除")
    @SysLog("删除")
    @PostMapping("/delete/{id}")
    @RequiresPermissions("sys:shortageOrder:delete")
    public Vo delete(@PathVariable @ApiParam(value = "id") Long id) throws AdminException {
        ShortageOrder orgPositionQuery = dao.get(ShortageOrder.class, id);
        if (orgPositionQuery == null) {
            throw new AdminException("无对应id对象");
        }

        orgPositionQuery.setDeleted(true);
        shortageOrderService.update(orgPositionQuery);
        return new Vo(HttpStatus.OK);
    }
}
